//
//  NSString+SCTimeFormat.m
//  Shuaiche
//
//  Created by dangshuai on 2019/2/1.
//  Copyright © 2019 zzs. All rights reserved.
//

#import "NSString+SCTimeFormat.h"

@implementation NSString (SCTimeFormat)

- (NSString *)formatStringByHourMiniteOrDayFromFormat:(NSString *)from {
    
    if (!from) {
        from = @"yyyy-MM-dd HH:mm:ss";
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:from];
    NSDate *timeDate = [dateFormatter dateFromString:self];
    
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    if((temp = timeInterval/60) <60){
        if (temp == 0) {
            temp = 1;
        }
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    } else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    } else {
        [dateFormatter setDateFormat:@"MM-dd"];
        result = [dateFormatter stringFromDate:timeDate];
    }
    return result;
}

- (NSInteger)yearIntevalStringFromFormat:(NSString *)from {
    if (!from) {
        from = @"yyyy-MM-dd";
    }
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:from];
    NSDate *oldDate = [dateFormatter dateFromString:self];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitYear;
    return [gregorian component:unitFlags fromDate:oldDate];
}

- (NSInteger)dayIntevalStringFromFormat:(NSString *)from {
    
    if (!from) {
        from = @"yyyy-MM-dd";
    }
    
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:from];
    NSDate *oldDate = [dateFormatter dateFromString:self];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitDay;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:currentDate  options:0];
    return [comps day]+1;
}

- (NSInteger)secondsIntevalStringFromFormat:(NSString *)from {
    if (!from) {
        from = @"yyyy-MM-dd";
    }
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:from];
    NSDate *oldDate = [dateFormatter dateFromString:self];
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitSecond;
    NSDateComponents *comps = [gregorian components:unitFlags fromDate:oldDate toDate:currentDate options:0];
    return comps.second;
}

- (NSString *)changeDateFormatWithFinalFormat: (NSString *)finalFormat {
    if (!finalFormat) {
        finalFormat = @"yyyy-MM-dd HH:mm:ss";
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:finalFormat];
    NSDate *date = [dateFormatter dateFromString:self];
    if (date) {
        [dateFormatter setDateFormat:finalFormat];
        return [dateFormatter stringFromDate:date];
    }
    return self;
}

 - (void)arrangeInWeekWithFromat:(NSString *)format startDate:(NSDate **)start endDate:(NSDate **)endDate {
    NSDate *date = [self dateFromFormatter:format];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    unsigned int unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday;
    NSDateComponents *compent = [calendar components:unitFlags fromDate:date];
    NSInteger currentWeekday = compent.weekday;
    
    NSDateComponents *adding = [[NSDateComponents alloc] init];
    if (compent.weekday == 1) { // 当天是周日
        [adding setWeekday:-6];
        *start = [calendar dateByAddingComponents:adding toDate:date options:0];
        *endDate = date;
    } else {
        [adding setWeekday:2 - currentWeekday];
        NSDate *lastDate = [calendar dateByAddingComponents:adding toDate:date options:0];
        [adding setWeekday:6];
        NSDate *featureDate = [calendar dateByAddingComponents:adding toDate:lastDate options:0];
        *start = lastDate;
//        if ([date compare:featureDate] == NSOrderedDescending) {
//            *endDate = date;
//        } else {
            *endDate = featureDate;
//        }
    }
}

- (NSDate *)dateFromFormatter:(NSString *)formatter {
    if (!formatter) {
        formatter = @"yyyy-MM-dd HH:mm:ss";
    }
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:formatter];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
    NSDate *date = [dateFormatter dateFromString:self];
    return date;
}

@end
